<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://unpkg.io/sockjs-client@1.5.0/dist/sockjs.min.js"></script>
  <script src='https://unpkg.io/@vertx/eventbus-bridge-client.js@1.0.0-1/vertx-eventbus.js'></script>
  <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" ></script>
  <script>
    function setCookie(cname,cvalue,exdays)
    {
      var d = new Date();
      d.setTime(d.getTime()+(exdays*24*60*60*1000));
      var expires = "expires="+d.toGMTString();
      document.cookie = cname + "=" + cvalue + "; " + expires;
    }
    var mydata = undefined;
    var eb = new EventBus('https://windows.prettyclass.top:1443/eventbus')
    eb.onopen = ()=> {
      $.get("/gen-id", function (data) {
        mydata = data.toString()
        eb.publish("user.hello", data.toString())
        $("p#sbm").text("你的识别码："+mydata)

        function f(){
          eb.publish("user.exit",mydata)
          eb.close()
        }
        eb.registerHandler("user.connect."+mydata,(err,msg)=>
          {

            $("div#accept").append("<p>收到"+msg.body+"的邀请</p><button onclick='f1(\""+msg.body+"\")'>accept</button>")

          }
        )
        eb.registerHandler("user.accept."+mydata,(err,msg)=>{

          $("body").children().remove()
          $("body").append("<video autoplay width='100%' height='100%' id='play'></video>")

          openwebrtc(true,msg.body)
        })
      })


    }
    function f(){
    }
    function f1(msg) {
      eb.publish("user.accept."+msg,mydata)
      $("body").children().remove()
      $("body").append("<video autoplay width='100%' height='100%' id='play'></video>")
      openwebrtc(false,msg)
    }
    function openwebrtc(first,oid){
     const pc = new RTCPeerConnection()
      pc.addEventListener("icecandidate",(ev)=>{
        eb.publish("user.ice."+oid,ev.candidate)
      })
      eb.registerHandler("user.ice."+mydata,(err,msg)=>{
        pc.addIceCandidate(msg.body)
      })
      pc.onaddstream=(ev)=>{
        let e = document.getElementById("play")
        e.srcObject= ev.stream
        console.log("fin")
        e.play()
      }

      if(first){
        pc.createOffer().then((offer)=>{
          eb.publish("user.offer."+oid,offer)
        })
        eb.registerHandler("user.answer."+mydata,(err,msg)=>{
          pc.setRemoteDescription(msg.body)
          navigator.mediaDevices.getUserMedia({audio:true,video:true}).then((st)=>{
            pc.addStream(st)
          })
        })
      }else{
        eb.registerHandler("user.offer."+mydata,(err,msg)=>{
          pc.setRemoteDescription(msg.body)
          pc.createAnswer().then((answer)=>{
            eb.publish("user.answer."+oid,answer)
            pc.setLocalDescription(answer)
          })
          navigator.mediaDevices.getUserMedia({audio:true,video:true}).then((st)=>{
            pc.addStream(st)
          })
        })

      }
    }
    function connect1() {
      eb.publish("user.connect."+$("input#b")[0].value,mydata)
    }

  </script>
</head>
<body onunload="f()">
<p id="sbm">你的识别码：</p>
<p>输入对方的识别码已连接</p><input list="b1" id="b">
<datalist id="b1">

</datalist>
<br/>
<br/>
<br/>
<br/>
<br/>
<div id="accept">

</div>
<button onclick="connect1()">连接</button>
</body>
</html>
